﻿namespace AuroraLightsFX
{
    //using AlienLabs.AlienFX.Tools;
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    
    public class LedController
    {

        public enum ActionEnum : byte
        {
            Loop = 4,
            Morph = 1,
            PlayPowerStatus = 15,
            Pulse = 2,
            SetColor = 3
        }



        public static ThemeDataset.ActionsRow CreateAction(int zone, int Color1, int Color2, ActionEnum Type, int duration)
        {
            ThemeDataset.ActionsRow row = new ThemeDataset.ActionsDataTable().NewActionsRow();
            row.ZoneID = zone;
            row.Color1 = string.Format("#{0:X8}", Color1);
            row.Color2 = string.Format("#{0:X8}", Color2);
            row.ActionTypeID = (int) Type;
            row.ActionID = (int) Type;
            row.Duration = duration;
            return row;
        }

        public static Led GetLed(LedName ledName)
        {
            return new Led(ledName.ToString(), (int) ledName, "");
        }




        public static bool SetColor(LedSetting[] settings)
        {
            bool retFlag = LedControlManager.SendResetCommand(true);

            if (retFlag)
            {
                for (int i = 0; i < settings.Length; i++)
                {
                    if ((((settings[i].action != ActionEnum.Loop) && (settings[i].action != ActionEnum.Morph)) && ((settings[i].action != ActionEnum.PlayPowerStatus) && (settings[i].action != ActionEnum.Pulse))) && (settings[i].action != ActionEnum.SetColor))
                    {
                        settings[i].action = ActionEnum.SetColor;
                    }
                    PacketData ledsGroup = new PacketData(i.ToString(), i, false);
                    ledsGroup.AddLed(settings[i].led);
                    ledsGroup.SetActions(new ThemeDataset.ActionsRow[] { CreateAction(1, settings[i].OnPowerColor.ToArgb(), settings[i].OnBatteryColor.ToArgb(), settings[i].action, settings[i].duration) }, true);
                    GroupPacket packet = new GroupPacket(ledsGroup);
                    retFlag=LedControlManager.SendPacket(packet, true);

                    //TODO: WorkItem# 522 
                    //LedControlManager.SendPacketToROM(packet);
                    
                    
                }
                //TODO: WorkItem# 522
                //LedControlManager.SendEmptyPacketToROM();
                retFlag=LedControlManager.SendStart();
                
            }
            
            return retFlag;
        }

 

        public static LedSetting CreateSetting(Led led,Color clr)
        {
            return CreateSetting(ActionEnum.SetColor, 0, led, clr, clr);
        }
        public static LedSetting CreateSetting(ActionEnum action, Led led, Color clr)
        {
            return CreateSetting(action, 0, led, clr, clr);
        }
        public static LedSetting CreateSetting(ActionEnum action, int duration, Led led, Color clr)
        {
            return CreateSetting(action, duration, led, clr, clr);
        }

        public static LedSetting CreateSetting(ActionEnum action, int duration, Led led, Color batteryColor, Color powerColor)
        {
            LedSetting setting = new LedSetting();
            setting.action = action;
            setting.duration = duration;
            setting.led = led;
            setting.OnBatteryColor = batteryColor;
            setting.OnPowerColor = powerColor;
            return setting;

        }

        public static LedSetting[] CreateSetting(Led led, Color startColor, Color endColor)
        {

            int startRed = 255;
            int startGreen = 0;
            int startBlue = 0;

            int deltaRed = 0;
            int deltaGreen = 0;
            int deltaBlue = 0;

            int numSteps = 3;

            int ctr=0;
            startRed = startColor.R;
            startGreen = startColor.G;
            startBlue = startColor.B;

            deltaRed = endColor.R - startRed;
            deltaGreen = endColor.G - startGreen;
            deltaBlue = endColor.B - startBlue;

            LedSetting[] settings = new LedSetting[(numSteps * 2)-2];

            Color currentColor = Color.Black;

            for (int stp = 0; stp < numSteps-1; stp++)
            {
                settings[ctr] = CreateSetting(led, Color.FromArgb(startRed + (deltaRed * stp) / numSteps,
                   startGreen + (deltaGreen * stp) / numSteps,
                   startBlue + (deltaBlue * stp) / numSteps));
                settings[ctr].duration = 0;
                ctr++;
            }

            for (int stp = numSteps-2; stp >= 0; stp--)
            {
                settings[ctr] = CreateSetting(led, Color.FromArgb(startRed + (deltaRed * stp) / numSteps,
                   startGreen + (deltaGreen * stp) / numSteps,
                   startBlue + (deltaBlue * stp) / numSteps));
                settings[ctr].duration = 0;
                ctr++;
            }
            return settings;
        }

        //public static bool SetColorToEEprom(LedSetting[] settings)
        //{
            
        //    if (LedControlManager.SendSpecialReset(LEDcontrol.ResetModifiers.Leave_Lights_On, 0))
        //    {
        //        return false;
        //    }
        //    foreach (PacketData data in defLedsGroup)
        //    {
        //        GroupPacket packet;
        //        try
        //        {
        //            packet = new GroupPacket(data);
        //        }
        //        catch (GroupPacketException exception)
        //        {
        //            LogManager.Log(AlienLabs.AlienFX.Tools.LogLevel.Info, "Creating Packet function", string.Concat(new object[] { "Packet Not Created", exception.CustomMessage, " -- ", DateTime.Now }));
        //            break;
        //        }
        //        foreach (byte[] buffer in packet.GroupActionsInfoCommands)
        //        {
        //            if (buffer.Length != 9)
        //            {
        //                return false;
        //            }
        //            LogManager.Log(AlienLabs.AlienFX.Tools.LogLevel.Info, " Command sent to EEPROM ", string.Format("{0:x2} {1:x2} {2:x2} {3:x2} {4:x2} {5:x2} {6:x2} {7:x2} {8:x2}", new object[] { buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7], buffer[8] }));
        //            byte[] send = this.CopyBytes(buffer, 1, 8);
        //            if (!this.EEpromWrite(visualizationItemType, 0, buffer.Length - 1, send))
        //            {
        //                return false;
        //            }
        //        }
        //    }
        //    return this.sendEmptyVisualizationPackageToEEprom(visualizationItemType);
        //}

        public static LedSetting[] CreateSetting(Led led)
        {

            LedSetting[] settings = new LedSetting[]{
                new LedSetting(),
                new LedSetting(),
                new LedSetting(),

            };

            settings[0].led = led;
            settings[0].action = ActionEnum.SetColor;
            settings[0].OnPowerColor = Color.Red;

            settings[1].led = led;
            settings[1].action = ActionEnum.SetColor;
            settings[1].OnPowerColor = Color.Green;

            settings[2].led = led;
            settings[2].action = ActionEnum.SetColor;
            settings[2].OnPowerColor = Color.Blue;

            return settings;
        }

        public static bool TurnOff()
        {
            bool flag = false;
            PacketData ledsGroup = new PacketData("1", 1, false);
            ledsGroup.AddLed(new Led(LedName.AlienHead.ToString(), 8, LedName.AlienHead.ToString()));
            ledsGroup.AddLed(new Led(LedName.AlienName.ToString(), 7, LedName.AlienName.ToString()));
            ledsGroup.AddLed(new Led(LedName.Keyboard.ToString(), 10, LedName.Keyboard.ToString()));
            ledsGroup.AddLed(new Led(LedName.LightPipe.ToString(), 5, LedName.LightPipe.ToString()));
            ledsGroup.AddLed(new Led(LedName.TouchPanel.ToString(), 0x10, LedName.TouchPanel.ToString()));
            ledsGroup.AddLed(new Led(LedName.Mouse.ToString(), 0, LedName.Mouse.ToString()));
            ThemeDataset.ActionsRow[] actions = new ThemeDataset.ActionsRow[1];
            int num = 0;
            int num2 = 0;
            actions[0] = CreateAction(1, num, num2, ActionEnum.SetColor, 0);
            ledsGroup.SetActions(actions, true);
            GroupPacket packet = new GroupPacket(ledsGroup);
            LedControlManager.SendResetCommand(true);
            if (LedControlManager.SendPacket(packet))
            {
                LedControlManager.SendStart();
                flag = true;
            }
            return flag;
        }

        public enum LedName
        {
            //FOR LAPTOP
            AlienHead = 8,
            AlienName = 7,
            Keyboard = 10,
            LightPipe = 5,
            TouchPanel = 16,
            Mouse = 0,
            PowerButton = 20,

            //FOR DESKTOP
            BottomGills=7,
            MiddleGills=8,
            FrontUSB=17,
            PowerRim=18,
            TopGills=9,
            FrontIO=16,
            AlienEyes=19,
            LeftAlienHead=6,
            RightAlienHead=13,
            FrontBays=14

        }

        [StructLayout(LayoutKind.Sequential)]
        public struct LedSetting
        {
            public Led led;
            public Color OnPowerColor;
            public Color OnBatteryColor;
            public ActionEnum action;
            public int duration;
        }
    }
}

